﻿Ext.define("PSI.OutPlan.TraceComments", {
    createUI: function (entity) {
        var me = this;
        me.entity = entity;

        var wnd = Ext.create("Ext.window.Window", {
            title: "服务评价",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 420,
            height: 580,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 100,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "id",
                            value: entity.id
                        },
                        {
                            xtype: "displayfield",
                            value: entity.planNumber,
                            fieldLabel: "计划号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.address,
                            fieldLabel: "门点"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.cntNumber,
                            fieldLabel: "箱号/封号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.cntType,
                            fieldLabel: "箱型"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.takeCntDT,
                            fieldLabel: "装箱时限"
                        },
                        {
                            id: "editTruckCompany",
                            fieldLabel: "拖车公司",
                            xtype: "displayfield",
                            value: entity.truckCompany
                        },
                        {
                            id: "editTruckNumber",
                            xtype: "displayfield",
                            fieldLabel: "车号/司机/电话",
                            value: entity.truckNumber + " " + entity.driver + " " + entity.driverTel
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划到装货地时间",
                            value: entity.dt0Plan
                        },
                        {
                            xtype: "displayfield",
                            fieldLabel: "实际到装货地时间",
                            value: entity.dt0Driver
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划装箱开始时间",
                            value: entity.dt1Plan
                        },
                        {
                            xtype: "displayfield",
                            fieldLabel: "实际装箱开始时间",
                            value: entity.dt1Driver
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划装货完成时间",
                            value: entity.dt2Plan
                        },
                        {
                            xtype: "displayfield",
                            fieldLabel: "实际装货完成时间",
                            value: entity.dt2Driver
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划回港时间",
                            value: entity.dt3Plan
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "实际回港时间",
                            value: entity.dt3Driver
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "计划完成时间",
                            value: entity.dt4Plan
                        }, {
                            xtype: "displayfield",
                            fieldLabel: "实际完成时间",
                            value: entity.dt4Driver
                        }, {
                            id: "editSrvLevel",
                            xtype: "combobox",
                            fieldLabel: "服务评级",
                            store: Ext.create("Ext.data.Store", {
                                fields: ["kf"],
                                data: [{ kf: "优" }, { kf: "良" }, { kf: "中" }, { kf: "差" }]
                            }),
                            queryMode: "local",
                            displayField: "kf",
                            valueField: "kf",
                            allowBlank: false,
                            blankText: "没有服务评级",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            forceSelection: true,
                            name: "srvLevel"
                        }, {
                            id: "editSrvClientsMemo",
                            xtype: "textareafield",
                            fieldLabel: "客户意见",
                            name: "srvClientsMemo"
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: me.onOK,
                            scope: me
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: me
                        }
                    ]
                }
            ]

        });

        me.wnd = wnd;
    },

    show: function (parentForm, entity) {
        var me = this;

        me.parentForm = parentForm;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(entity); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (entity) {

        var me = this;
        var form = me.wnd;
        var el = me.wnd.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/OutPlan/GetTraceComments",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    me.setEntityData(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });
    },

    setEntityData: function (data) {
        Ext.getCmp("editSrvLevel").setValue(data.srvLevel);
        Ext.getCmp("editSrvClientsMemo").setValue(data.srvClientsMemo);
    },

    // private
    onOK: function () {
        var me = this;

        var f = Ext.getCmp("editForm");
        var el = f.getEl() || Ext.getBody();
        el.mask("数据保存中...");
        f.submit({
            url: "/OutPlan/TraceComments",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                me.wnd.close();
                me.parentForm.refreshPlanGrid();
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editSrvLevel").focus();
                });
            }
        });
    }
});